Exercise 1

Choose the correct answer

  1. Which statement is used to display output in Python?
    a) input()   b) print()   c) display()   d) output()

    Answer: b) print()

  2. Which of the following is NOT a valid Python data type mentioned in the text?
    a) int   b) float   c) character   d) bool

    Answer: c) character

  3. What type of values does the Boolean data type hold?
    a) Whole numbers   b) Decimal numbers   c) Text   d) True or False

    Answer: d) True or False

  4. Which of the following is an example of a relational operator?
    a) +   b) *   c) ==   d) and

    Answer: c) ==

  5. Which logical operator returns True if both conditions are true?
    a) or   b) not   c) and   d) !=

    Answer: c) and

  6. What is the purpose of the if statement in Python?
    a) To repeat a block of code.
    b) To define a function.
    c) To execute a block of code only if a condition is true.
    d) To store multiple items in a single variable.

    Answer: c) To execute a block of code only if a condition is true.

  7. Which conditional statement allows you to check multiple conditions in sequence?
    a) if   b) if-else   c) nested if   d) if-elif-else

    Answer: d) if-elif-else

  8. What is the term for repeating a block of code multiple times?
    a) Selection   b) Iteration   c) Condition   d) Assignment

    Answer: b) Iteration

  9. Which type of loop is used when you know the number of times you want to repeat a block of code?
    a) while loop   b) for loop   c) if loop   d) nested loop

    Answer: b) for loop

  10. Which data structure is used to store multiple items in a single variable in Python, as shown in the example ["Computer", "Science", 20, True]?
    a) String   b) Tuple   c) List   d) Dictionary

    Answer: c) List

Write short answers to these questions

  1. What is the primary function of the input() function in Python?

    It pauses the program, lets the user type something, and hands that typing back as a string.

  2. Provide an example of a string literal in Python.
    name = "Bhaktapur1"
  3. What is an identifier in the context of Python programming?

    An identifier is the name given to a program unit such as a variable, a function or a class.

  4. Explain the difference between the division operator (/) and the floor division operator (//) in Python.

    / keeps the decimal part, // throws it away and floors the answer.

    print(9 / 2)   # 4.5
    print(9 // 2) # 4
  5. Define the concept of data types in Python.

    A data type is the kind of value a variable holds, such as int, float, str or bool.

  6. What is the purpose of relational operators in Python? Give one example.

    They compare two values and answer with True or False, like 7 > 5 which gives True.

  7. Describe the functionality of the else block in an if-else statement.

    The else block runs only when the if condition turns out False.

  8. What is a nested if statement?

    It is an if statement written inside another if statement, so the inner one is only reached when the outer condition is True.

  9. When would you typically use a for loop instead of a while loop?

    When the number of repetitions is already known, like walking through range(5) or a list.

  10. What is the role of the range() function often used with for loops?

    It produces the sequence of numbers that the for loop walks over.

  11. Define a Python list and provide a simple example.

    A list is a built-in type that stores many values in one variable.

    marks = [10, 20, 30]
  12. Discuss the concept of iteration in programming.

    Iteration is repeating a task again and again until a stopping condition is satisfied. Python does it with for and while loops.

Write long answers to these questions

  1. Explain the different categories of operators available in Python (arithmetic, relational, and logical) with examples of each.

    Arithmetic operators do maths, relational operators compare values and answer True or False, and logical operators join those answers together.

    print(10 + 20)        # arithmetic, gives 30
    print(10 > 20) # relational, gives False
    print(10 > 5 and 3 < 4) # logical, gives True
  2. Describe the three main types of conditional statements in Python (if, if-else, and if-elif-else).

    if runs a block only when its condition is True. if-else adds a fallback block for when it is False. if-elif-else checks many conditions in order and runs the first one that is True.

    marks = int(input("Marks: "))
    if marks >= 80:
    print("Distinction")
    elif marks >= 40:
    print("Pass")
    else:
    print("Fail")
  3. Explain the two types of loops available in Python (for and while) with their respective syntaxes and a practical example of each.

    A for loop walks through a known sequence. A while loop keeps going as long as its condition stays True.

    for i in range(1, 4):
    print("Round", i)

    count = 1
    while count <= 3:
    print("Round", count)
    count += 1

    Output

    Round 1
    Round 2
    Round 3
    Round 1
    Round 2
    Round 3
  4. Describe the four basic data types covered in the text (integer, float, string, and boolean), providing an example of each. Why is it important to understand data types when writing Python programs?
    age = 26          # int
    height = 6.0 # float
    name = "Manish" # str
    is_student = True # bool

    Types decide what an operation means. "2" + "2" joins text into "22" while 2 + 2 adds numbers into 4, so the wrong type quietly gives the wrong answer or a TypeError.

  5. Imagine you need to write a Python program that takes an integer input from the user and performs the following actions: prints "Positive" if the number is greater than 0, "Negative" if it is less than 0, "Zero" if it is equal to 0, and if the number is positive, it then checks whether it is even or odd and prints the result.
    num = int(input("Enter a number: "))
    if num > 0:
    print("Positive")
    if num % 2 == 0:
    print("Even")
    else:
    print("Odd")
    elif num < 0:
    print("Negative")
    else:
    print("Zero")

    Output

    Enter a number: 7
    Positive
    Odd